<?php
/**
 * Created by PhpStorm.
 * User: CodeShuhao
 * Date: 2019/6/9
 * Time: 17:47
 */

namespace app\admin\controller;

use app\admin\service\LoginService;
use app\common\controller\BasicController;
use app\common\validate\UserValidate;

class LoginController extends BasicController
{
    private $login = null;

    private $validate = null;

    public function __construct()
    {
        parent::__construct();

        $this->login = new LoginService();

        $this->validate = new UserValidate();
    }

    /**
     * @action 账户登录
     */
    public function login()
    {
        $this->isPost();

        return $this->fetch();
    }

    /**
     * @action 账户登录验证
     */
    protected function loginpost()
    {
        $request = input('post.');

        //验证数据
        if ( $this->validate->scene('login')->check($request) === false )
        {
            $this->failure($this->validate->getError());
        }

        //验证登录
        if ( $this->login->login($request) === false )
        {
            $this->failure('登录失败:' . $this->login->getError());
        }

        $this->render('登录成功', url('index/index'));
    }

    /**
     * 登出
     */
    public function logout()
    {
        session('uid',null);

        $this->redirect('login');
    }
}